package com.jarvis.app.car.service;

import com.jarvis.app.car.common.MessageResult;
import com.jarvis.app.car.model.CarTrack;
import com.jarvis.app.car.model.Ticket;
import com.jarvis.app.car.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class TicketService {

    public MessageResult<List<Ticket>> listByUserId(Long userId) {
        ArrayList<Ticket> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "select * from ticket where user_id = ? order by create_time desc";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setLong(1, userId);
            resultSet = pst.executeQuery();

            while (resultSet.next()) {
                long id = resultSet.getLong("id");
                String issue = resultSet.getString("issue");
                String answer = resultSet.getString("answer");
                String date = resultSet.getString("create_time");
                LocalDateTime dateTime = null;

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
                    dateTime = LocalDateTime.parse(date, dateTimeFormatter);
                }


                Ticket ticket = new Ticket(id, issue, answer, userId, dateTime);
                list.add(ticket);
            }
        } catch (Exception e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }


        return MessageResult.ok(null, list);

    }


    public MessageResult<Ticket> getById(Long ticketId) {
        Ticket ticket = null;
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "select * from ticket where id = ? order by create_time desc";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setLong(1, ticketId);
            resultSet = pst.executeQuery();

            while (resultSet.next()) {
                long id = resultSet.getLong("id");
                Long userId = resultSet.getLong("user_id");
                String issue = resultSet.getString("issue");
                String answer = resultSet.getString("answer");
                String date = resultSet.getString("create_time");
                LocalDateTime dateTime = null;

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
                    dateTime = LocalDateTime.parse(date, dateTimeFormatter);
                }


                ticket = new Ticket(id, issue, answer, userId, dateTime);

            }
        } catch (Exception e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }


        return MessageResult.ok(null, ticket);

    }

    public MessageResult<List<Ticket>> listNoAnswer() {
        ArrayList<Ticket> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "select * from ticket where answer is null or answer='' order by create_time desc";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            resultSet = pst.executeQuery();

            while (resultSet.next()) {
                long id = resultSet.getLong("id");
                String issue = resultSet.getString("issue");
                String answer = resultSet.getString("answer");
                String date = resultSet.getString("create_time");
                Long userId = resultSet.getLong("user_id");

                LocalDateTime dateTime = null;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
                    dateTime = LocalDateTime.parse(date, dateTimeFormatter);
                }

                Ticket ticket = new Ticket(id, issue, answer, userId, dateTime);
                list.add(ticket);
            }
        } catch (Exception e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }

        return MessageResult.ok(null, list);
    }


    public MessageResult<Void> answer(Long ticketId, String answer) {
        ArrayList<Ticket> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "update ticket set answer=? where id=?";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setString(1, answer);
            pst.setLong(2, ticketId);

            int i = pst.executeUpdate();
            if (i > 0) {
                return MessageResult.ok();
            }


        } catch (Exception e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }

        return MessageResult.fail(null, null);
    }


    public MessageResult<Void> create(Long accountId, String issue) {
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "insert into ticket(issue,user_id) values(?,?) ";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setString(1, issue);
            pst.setLong(2, accountId);
            pst.executeUpdate();


        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }


        return MessageResult.ok();
    }
}
